var util = require('../../utils/util.js');
const app = getApp()
var pageNum = 1;
var searchTitle = ""; // 搜索关键字
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showView: false,
        msgList: [],
        loading: false,
        loadend: false,
        searchLogList: [],      // 存储搜索历史记录信息
        hidden: true,           // 加载提示框是否显示
        inputShowed: false,     // 搜索输入框是否显示
        inputVal: "",           // 搜索的内容
        searchLogShowed: false, // 是否显示搜索历史记录
        searchtype:0,
        showModalStatus: false,
        animationData: '',
        isLoginAuth: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        pageNum = 1;
      if (!app.globalData.isLoginAuth) {
        this.showModal();
      }
      this.setData({
        isLoginAuth: app.globalData.isLoginAuth
      })
       // console.log(options);
        if (options.keyword!=''){
            searchTitle = options.keyword;
            this.setData({
                msgList: [],
                searchLogShowed: false,
                inputVal: searchTitle
            });
            this.loadMsgData();
        }

    },
    //滚动到底部刷新
    onReachBottom: function () {
        this.loadMsgData();
    },
    onShow: function () {
        var that = this;
        pageNum = 1;
        that.setData({
            msgList: []
        })
      //  this.loadMsgData();
    },
    //获取教练和驾校信息
    loadMsgData: function () {
        var that = this;
        var limit = 6;
        var url = app.globalData.hostUrl + 'Common/carlist';
        wx.request({
            url: url,
            data: {
                page: pageNum,
                psize: limit,
                keywd: searchTitle
            },
            success: function (res) {
//                console.log(res);
                if (res.data.result) {
                    pageNum++
                    that.setData({
                        msgList: that.data.msgList.concat(res.data.result)
                    });
                    if (res.data.result.length <= limit) {
                        that.setData({
                            loading: false,
                            loadend:true
                        })
                    }
                }
                else {
                    that.setData({
                        loading: false,
                        loadend: true
                    })
                    return false
                }
            }
        })
    },
    // 显示搜索输入框和搜索历史记录
    showInput: function () {
        var that = this;
        if ("" != wx.getStorageSync('searchLog')) {
            that.setData({
                inputShowed: true,
                searchLogShowed: true,
                searchLogList: wx.getStorageSync('searchLog')
            });
        } else {
            that.setData({
                inputShowed: true,
                searchLogShowed: true
            });
        }

    },
    // 输入内容时 把当前内容赋值给 查询的关键字，并显示搜索记录
    inputTyping: function (e) {
        var that = this;
        // 如果不做这个if判断，会导致 searchLogList 的数据类型由 list 变为 字符串
        if ("" != wx.getStorageSync('searchLog')) {
            that.setData({
                inputVal: e.detail.value,
                searchLogList: wx.getStorageSync('searchLog')
            });
        } else {
            that.setData({
                inputVal: e.detail.value,
                searchLogShowed: true
            });
        }
        searchTitle = e.detail.value;
    },
    // 显示搜索历史记录
    searchLogShowed: function () {
        var that = this;
        if ("" != wx.getStorageSync('searchLog')) {
            that.setData({
                searchLogShowed: true,
                searchLogList: wx.getStorageSync('searchLog')
            });
        } else {
            that.setData({
                searchLogShowed: true
            });
        }
    },
    // 点击 搜索 按钮后 隐藏搜索记录，并加载数据
    searchData: function () {
        var that = this;
        that.setData({
            msgList: [],
            searchLogShowed: false,
            loading: true,
        });
        pageNum = 1;
        this.loadMsgData();
        // 搜索后将搜索记录缓存到本地
        if ("" != searchTitle) {
            var searchLogData = that.data.searchLogList;
            searchLogData.push(searchTitle);
            wx.setStorageSync('searchLog', searchLogData);
        }
    },
    // 通过搜索记录查询数据
    searchDataByLog: function (e) {
        // 从view中获取值，在view标签中定义data-name(name自定义，比如view中是data-log="123" ; 那么e.target.dataset.log=123)
        searchTitle = e.target.dataset.log;
        var that = this;
        that.setData({
            msgList: [],
            searchLogShowed: false,
            inputVal: searchTitle
        });
        pageNum = 1;
        this.loadMsgData();
    },
    // 点击叉叉icon 清除输入内容，同时清空关键字，并加载数据
    clearInput: function () {
        var that = this;
        that.setData({
            msgList: [],
            inputVal: ""
        });
        searchTitle = "";
        pageNum = 1;
        this.loadMsgData();
    },
    // 清楚搜索记录
    clearSearchLog: function () {
        var that = this;
        that.setData({
            hidden: false
        });
        wx.removeStorageSync("searchLog");
        that.setData({
            searchLogShowed: false,
            hidden: true,
            searchLogList: []
        });
    },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else if (e.detail.errMsg == "getUserInfo:fail auth deny") {

    }
    else {
      wx.redirectTo({
        url: '../xuecar/index',
      })
    }
  }
})